import { TeamSeriveProvider } from './../../../providers/team-serive/team-serive';
import { PcSeriveProvider } from "./../../../providers/pc-serive/pc-serive";
import { Component } from "@angular/core";
import {
  IonicPage,
  NavController,
  NavParams,
  ToastController,
  LoadingController
} from "ionic-angular";
import { BaseUI } from "../../../common/baseUI";

/**
 * Generated class for the PcPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: "page-pc",
  templateUrl: "pc.html"
})
export class PcPage extends BaseUI {
  pcDatas = 0;
  allEval;
  ifshow = [];
  tday;//当前日期
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    private pcSerive: PcSeriveProvider,
    public loadingCtrl: LoadingController,
    private toastCtrl: ToastController,
    public teamSeriveProvider: TeamSeriveProvider,
  ) {
    super();
  }
  //判断时间状态是否开启评测
  timestate() {
    for (let i = 0; i < this.allEval.length; i++) {
      this.tday = new Date(this.tday)
      let rday = new Date(this.allEval[i]["evaluatingtime"]);//获取开始天
      let eday = new Date(this.allEval[i]["evaluatingendtime"]);//获取结束天      

      rday.setHours(0, 0, 0);//更改开始天的时间
      eday.setHours(23, 59, 59);//更改结束天的时间
      if (rday.valueOf() <= this.tday.valueOf() && this.tday.valueOf() <= eday.valueOf()) {
        this.ifshow[i] = 1; //开启
      } else if (rday.valueOf() > this.tday.valueOf() && this.tday.valueOf() <= eday.valueOf()) {
        this.ifshow[i] = 2; //未开始
      } else {
        this.ifshow[i] = 3; //结束
      }
    }
  }
  //页面跳转
  openSmitem(id) {
    if (this.ifshow[id] == 1) {
      this.navCtrl.push("PcSmItemPage", {
        evaluatingCode: this.allEval[id]["evaluatingcode"],
        teamImg: this.allEval[id]["teamicon"]
      });
    } else if (this.ifshow[id] == 2) {
      this.showToast(this.toastCtrl, "评测未开始");
    } else {
      this.navCtrl.push("PcReportPage", {
        teamCode: this.allEval[id]["teamcode"],
        evalcode: this.allEval[id]["evaluatingcode"]
      });
    }
  }

  ionViewWillEnter() {
    //填加团队背景图片
    this.teamSeriveProvider.getTeamAll().subscribe(data => {
      let teamimg = data;
      if (!localStorage.getItem('teamimg')) {
        for (let index = 0; index < teamimg.length; index++) {
          teamimg[index]['teambg'] = this.pcSerive.rand();
        }
        localStorage.setItem('teamimg', JSON.stringify(teamimg));
      }
    });


    if (this.pcDatas != 0) {
      this.pcSerive.getAllEvalList().subscribe(data => {
        this.tday = data['msg'];//获取当前天
        if (data['data']) {
          this.allEval = data['data'];;
          this.timestate();
          this.pcDatas = 1;
        } else if (!data['data'] && data['status'] == 200) {
          this.allEval = [];
          this.timestate();
          this.pcDatas = 1;
        } else {
          return null;
        }
      },
        err => {
          this.showToast(this.toastCtrl, "请检查网络连接之后重试");
        });
    } else {
      //进入时显示正在加载
      const loading = this.showLoading(this.loadingCtrl, "加载中");
      this.pcSerive.getAllEvalList().subscribe(data => {

        this.tday = data['msg'];//获取当前天 
        loading.dismiss();
        if (data['data']) {
          this.allEval = data['data'];
          this.timestate();
          this.pcDatas = 1;
        } else if (!data['data'] && data['status'] == 200) {
          this.allEval = [];
          this.timestate();
          this.pcDatas = 1;
        } else {
          return null;
        }
      },
        err => {
          this.showToast(this.toastCtrl, "请检查网络连接之后重试");
        });
    }
  }
}